package com.mazaiting.instructions.stack;

import com.mazaiting.instructions.base.NoOperandsInstruction;
import com.mazaiting.rtda.MtFrame;
import com.mazaiting.rtda.MtOperandStack;

/**
 * 出栈指令
 *
 * @author mazaiting
 * @since 2021/8/14
 */
public class PopInstruction {

    /**
     * 出栈
     * 只能用于弹出int、float等占用一个操作数栈位置的变量
     * bottom -> top
     * [...][c][b][a]
     *             |
     *             V
     * [...][c][b]
     */
    public static class Pop extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            frame.getOperandStack().popSlot();
        }
    }

    /**
     * 出栈
     * double和long变量在操作数栈中占据两个位置，需要使用pop2指令弹出
     * bottom -> top
     * [...][c][b][a]
     *          |  |
     *          V  V
     * [...][c]
     */
    public static class Pop2 extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            MtOperandStack stack = frame.getOperandStack();
            stack.popSlot();
            stack.popSlot();
        }
    }

}
